<?php
function getTime()
{
	list($usec, $sec) = explode(" ", microtime());
	return ((float)$usec + (float)$sec);
}

$stime = getTime();

include_once("include/sdk/DiskSearch.php");

$sch = new DiskSearch();
$config = $sch->getConfig();

include_once("include/Pager.php");
include_once("include/Template.php");

if(!file_exists("include/lang/lang.".$config["language"].".php")) {
	exit("您使用的语言包不存在，请下载或自行制作语言包!");
}

include_once("include/lang/lang.".$config["language"].".php");

$tpl = new Template($config["template"]);

$tpl->assign("config", $config);
$tpl->assign("stime", $stime);
$tpl->assign("support", $sch->getHostName());
$tpl->assign("lang", $lang);
$tpl->assign("copyright", $tpl->fetch("copyright.html"));

function get_rewrite_url($url, $rewrite = false) {
	global $config;

	//过滤异常
	if($url == "/search.php") {
		return $url;
	}

	if(preg_match("/type=rss/", $url)) {
		return $url;
	}

	if($rewrite) {
		if(preg_match("/(\w+)\.php$/", $url, $arr_match_list)) {
			return "/".$arr_match_list[1];
		}

		if(preg_match("/detail\.php\?fid=(\w+)/", $url, $arr_match_list)) {
			if(isset($config["detail_prefix"]) && $config["detail_prefix"] != "") {
				return "/{$config["detail_prefix"]}/".$arr_match_list[1]."/";
			}

			return "/file/".$arr_match_list[1]."/";
		}

		if(preg_match("/search\.php\?word=(.+)/", $url, $arr_match_list)) {
			if(preg_match("/search\.php\?word=([^&]*)$/", $url, $arr_match_list)) {
				$keyword = urldecode($arr_match_list[1]);
				$prefix  = substr($keyword, 0, 1);
				if(ord($prefix) < 0 || ord($prefix) > 127) {
					$prefix = urlencode(substr($keyword, 0, 3));
				}
				
				if(isset($config["search_prefix"]) && $config["search_prefix"] != "") {
					return "/{$config["search_prefix"]}/".$prefix."/".urlencode($keyword);
				}

				return "/".$prefix."/".urlencode($keyword);
			}

			if(preg_match("/search\.php\?word=([^&]+)&site=0&sort=1&range=0&cate=1&page=(\d+)/", $url, $arr_match_list)) {
				$keyword = urldecode($arr_match_list[1]);
				$prefix  = substr($keyword, 0, 1);
				$page    = intval($arr_match_list[2]);
				
				if(ord($prefix) < 0 || ord($prefix) > 127) {
					$prefix = urlencode(substr($keyword, 0, 3));
				}
				
				if(isset($config["search_prefix"]) && $config["search_prefix"] != "") {
					return $page > 1 ? "/{$config["search_prefix"]}/".$prefix."/".urlencode($keyword)."/".$page : "/{$config["search_prefix"]}/".$prefix."/".urlencode($keyword);
				}

				return $page > 1 ? "/".$prefix."/".urlencode($keyword)."/".$page : "/".$prefix."/".urlencode($keyword);
			}

			return $url;
		}
	}

	return $url;
}

//用来解决SEO前后策略变更的问题
if($_SERVER['REQUEST_URI'] !== $str_rewrite_url = get_rewrite_url($_SERVER['REQUEST_URI'], $config["rewrite"])) {
	header("HTTP/1.1 301 Moved Permanently") ; 
	header("location:$str_rewrite_url");
	exit;
}

$word = "";
if(isset($_GET["word"])) {
	$word = urldecode(trim(strip_tags($_GET["word"])));
	if($_COOKIE["word"] != $word) {
		setcookie("word", $word, time() + 24 * 60 * 60 * 365);
	}
} elseif(isset($_COOKIE["word"]) && !empty($_COOKIE["word"])) {
	//$word = $_COOKIE["word"];
}

$word = str_replace("\\", "", $word);

$tpl->assign("word", htmlspecialchars($word));

$sch->setDebug($config["debug"]);
?>